<?php
/**
 * Hesperos library for Zend Framework
 *
 * This script is protected by copyright. It's use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    Hesperos
 * @package     Registry
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     1.0 $Id$
 * @link        http://code.google.com/p/hesperos/
 */

/**
 * Rejestr sesji
 *
 * @category    Hesperos
 * @package     Registry
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @version     1.0 $Id$
 */
class Hesperos_Registry_Session
{
    /**
     * Klucz sesji
     *
     * @var string
     */
    static private $_key = '__Hesperos-reg__kf3Skf30feu29Vd30s2sGx2';

    /**
     * Zend_Session_Namespace instance
     *
     * @var Zend_Session_Namespace|null
     */
    static private $_session = null;

    /**
     * Zwraca obiekt sesji
     *
     * @return Zend_Session_Namespace
     */
    public static function getSession ()
    {
        if(self::$_session === null) {
            self::$_session = new Zend_Session_Namespace (sha1(self::$_key));
        }
        return self::$_session;
    }

    /**
     * Rejestruje wartość
     *
     * @param string $name nazwa
     * @param mixed $value
     * @return void
     */
    public static function set ($name, $value)
    {
        $session = self::getSession();
        $session->{$name} = $value;
    }

    /**
     * Zwraca flagę czy wartość istnieje
     *
     * @param string $name nazwa
     * @return boolean
     */
    public static function has ($name)
    {
        $session = self::getSession();
        return isset($session->{$name});
    }

    /**
     * Zwraca wartość
     *
     * @param string $name nazwa
     * @return mixed
     * @throws Hesperos_Exception
     */
    public static function get ($name)
    {
        if(!self::has($name)) {
            throw new Hesperos_Exception('Index "' . $name . '" not exists');
        }
        $session = self::getSession();
        return $session->{$name};
    }

    /**
     * Usuwa wartość
     *
     * @param string $name nazwa
     * @return mixed
     * @throws Hesperos_Exception
     */
    public static function del ($name)
    {
        if(!self::has($name)) {
            throw new Hesperos_Exception('Index "' . $name . '" not exists');
        }
        $session = self::getSession();
        unset($session->{$name});
    }
}